/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package databaseHandling;

import interfaces.Lager_VareInterface;
import interfaces.VareGruppeInterface;
import interfaces.VareInterface;
import java.rmi.RemoteException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Bruger;
import model.Lager_Vare;
import model.Vare;
import model.VareGruppe;

/**
 *
 * @author Lunde
 */
public abstract class LagerHandler {

    public static ArrayList<VareInterface> getAlleVarer() throws SQLException, RemoteException {

        ArrayList<VareInterface> varer = new ArrayList<>();

        ResultSet rs = DatabaseHandler.getInstance().select("CALL hentAlleVarer()");
        while (rs.next()) {
            varer.add(new Vare(rs.getInt("id"), rs.getString("titel"), rs.getString("beskrivelse"), rs.getInt("pris"), rs.getInt("antalReserveret"), rs.getInt("antalLager"), new VareGruppe(rs.getInt("VareGruppeId"), rs.getString("gruppeNavn"))));
        }
        return varer;
    }

    public static ArrayList<Lager_VareInterface> hentLagerTilknyttetBruger(Bruger bruger) throws SQLException, RemoteException {

        ArrayList<Lager_VareInterface> lagerVarer = new ArrayList<>();
        ResultSet rs = DatabaseHandler.getInstance().select("CALL HentVareFraLager(" + bruger.getId() + ")");
        while (rs.next()) {
            Vare vare = new Vare(rs.getInt("id"), rs.getString("titel"), rs.getString("beskrivelse"), rs.getInt("pris"), rs.getInt("antalReserveret"), rs.getInt("antalLager"), new VareGruppe(rs.getInt("VareGruppeId"), rs.getString("gruppeNavn")));
            lagerVarer.add(new Lager_Vare(rs.getInt("salgsPris"), rs.getInt("lagerVareAntal"), vare));
        }
        return lagerVarer;
    }

    public static ArrayList<VareGruppeInterface> hentAlleVareGrupper() throws SQLException, RemoteException {
        ArrayList<VareGruppeInterface> varegrupper = new ArrayList<>();

        ResultSet rs = DatabaseHandler.getInstance().select("CALL HentVareGrupper()");
        while (rs.next()) {
            varegrupper.add(new VareGruppe(rs.getInt("id"), rs.getString("gruppeNavn")));
        }
        return varegrupper;
    }

    public static VareInterface hentVareVedId(int id) throws SQLException, RemoteException {
        String sql = "CALL hentVareVedID(" + id + ")";

        ResultSet rs = DatabaseHandler.getInstance().select(sql);
        VareInterface vare = null;
        if (rs.next()) {
            VareGruppe vareGruppe = new VareGruppe(rs.getInt("VareGruppeId"), rs.getString("gruppeNavn"));
            vare = new Vare(rs.getInt("id"), rs.getString("titel"), rs.getString("beskrivelse"), rs.getInt("pris"), rs.getInt("antalReserveret"), rs.getInt("antalLager"), vareGruppe);
        }
        return vare;
    }
}
